package com.itheima.zhaohang.function;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.MediaController;
import com.itheima.zhaohang.livepalyonline.R;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.widget.VideoView;

public class VidoListPlay extends AppCompatActivity {

    private RelativeLayout mLoding;
    private VideoView      mVideoView;
    private static final int mPalyTimes = 5;
    private              int mConut     = 0;
    private String       mUri;
    private String       mName;
    private ImageView    mBack;
    private TextView     mTime;
    private LinearLayout mMainLayout;
    private LinearLayout mHandLayout;
    private LinearLayout mFootLayout;
    private static final int     HANDDISMISSTIME = 5000;
    private              Handler mHandler        = new Handler(Looper.getMainLooper());
    private ImageView mStart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vido_list_play);
        mLoding = (RelativeLayout) findViewById(R.id.loding);
        mVideoView = (VideoView) findViewById(R.id.surfceview);

        Intent intent = getIntent();
        mUri = intent.getStringExtra("uri");
        mName = intent.getStringExtra("name");



        //        //设置全屏播放
        MediaController controller = new MediaController(VidoListPlay.this);
//        mVideoView.setMediaController(controller);
        controller.setAnchorView(mVideoView);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.FILL_PARENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        mVideoView.setLayoutParams(layoutParams);
//        mVideoView.requestFocus();

        //        开始播放回调
        mVideoView.setVideoURI(Uri.parse(mUri));
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mVideoView.start();

            }
        });
        //缓存2M
        mVideoView.setBufferSize(1024 * 1024 * 2);
        //错误回调
        mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                if (mConut > mPalyTimes) {

                    new AlertDialog.Builder(VidoListPlay.this)
                            .setMessage("播放失败了")
                            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                    dialog.dismiss();
                                }
                            }).setCancelable(false).show();
                } else {

                    mVideoView.stopPlayback();
                    mVideoView.setVideoURI(Uri.parse(mUri));

                }

                mConut++;
                return false;
            }
        });
        //        是不是视频文件正在缓冲播放
        mVideoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                switch (what) {
                    //当正在加载中的时候，显示进度圈
                    case MediaPlayer.MEDIA_INFO_BUFFERING_START:
                        mLoding.setVisibility(View.VISIBLE);
                        break;
                    //加载全部完后才能
                    case MediaPlayer.MEDIA_INFO_BUFFERING_END:
                        //音频和视频不同步
                    case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING:
                        //边下边播
                    case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED:

                        mLoding.setVisibility(View.INVISIBLE);
                        break;
                }
                return false;
            }
        });


        mBack = (ImageView) findViewById(R.id.imageback);
        mTime = (TextView) findViewById(R.id.texttime);


        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mTime.setText(getTime() == null ? "当前时间无法获取" : getTime());
                    }
                });
                mHandler.postDelayed(this, 1000);
            }
        }, 1000);
        mTime.setText(getTime() == null ? "当前时间无法获取" : getTime());
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mHandLayout.setVisibility(View.INVISIBLE);
                        mFootLayout.setVisibility(View.INVISIBLE);
                    }
                });

                mHandler.removeCallbacks(this);
            }
        }, HANDDISMISSTIME);

        mMainLayout = (LinearLayout) findViewById(R.id.mianlayout);
        mHandLayout = (LinearLayout) findViewById(R.id.handlayout);
        mFootLayout = (LinearLayout) findViewById(R.id.footlayout);
        mStart = (ImageView) findViewById(R.id.start);
        if (mVideoView.isPlaying()) {
            mStart.setSelected(true);
        } else {
            mStart.setSelected(false);
        }

        mStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mVideoView.isPlaying()) {
                    mStart.setSelected(false);
                    mVideoView.stopPlayback();
                } else {
                    mStart.setSelected(true);
                    mVideoView.setVideoURI(Uri.parse(mUri));
                    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            mVideoView.start();

                        }
                    });
                }
            }
        });

        mMainLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mHandLayout.getVisibility() != View.VISIBLE || mFootLayout.getVisibility() != View.VISIBLE) {
                    mHandLayout.setVisibility(View.VISIBLE);
                    mFootLayout.setVisibility(View.VISIBLE);

                    if (mVideoView.isPlaying()) {
                        mStart.setSelected(true);
                    } else {
                        mStart.setSelected(false);
                    }


                } else {
                    mHandLayout.setVisibility(View.INVISIBLE);
                    mFootLayout.setVisibility(View.INVISIBLE);

                    return;
                }
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mHandLayout.setVisibility(View.INVISIBLE);
                                mFootLayout.setVisibility(View.INVISIBLE);

                            }
                        });

                        mHandler.removeCallbacks(this);
                    }
                }, HANDDISMISSTIME);
            }
        });
    }
    public String getTime() {
        //获取本地时间
        try {
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            String format = formatter.format(calendar.getTime());
            return format;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mVideoView != null) {
            mVideoView.stopPlayback();
        }
        mHandler.removeCallbacksAndMessages(null);
    }
}
